﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class SaleManagement : System.Web.UI.Page
{
   
    SaleDAO saleDAO = new SaleDAO();
    ExhibitionDAO exhDAO = new ExhibitionDAO();

    public string IsPayed(string obj)
    {
        string result = "Not yet";
        bool payed;
        if (obj != null || !obj.Equals(""))
        {
            if (obj == "True")
                payed = true;
            else payed = false;

            if (payed == true)
                result = "Paid";
        }
        return result;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
        if (Request["SalesID"] != null)
        {
            Control ctrl;
            ctrl = Page.LoadControl("~/Share/Insert_UpdateSales.ascx");
            pnlSales.Controls.Clear();
            pnlSales.Controls.Add(ctrl);
        }

       

    }


    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            SaleDAO DAOSale = new SaleDAO();
            SaleInfo saleInfo = new SaleInfo();
            bool ischeck = false;

            foreach (GridViewRow row in grd_PostingSold.Rows)
            {
                if (((CheckBox)row.FindControl("chk_Item")).Checked == true)
                {
                    // ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Please choose the exhibition to delete!');", true);
                    ischeck = true;
                }

            }

            if (ischeck == true)
            {
                foreach (GridViewRow row in grd_PostingSold.Rows)
                {
                    int saleID = int.Parse(grd_PostingSold.DataKeys[row.RowIndex].Value.ToString());

                    if (((CheckBox)row.FindControl("chk_Item")).Checked == true)
                    {

                        if (DAOSale.Delete(saleID) > 0)
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete successfull!');", true);
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete Failed!');", true);

                        }
                    }

                }
                LoadData();
            }
            else { ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Please choose the Competition to delete!');", true); }
        }
        catch (Exception ex) { }


    }

    public void LoadData()
    {
        PostingDAO postDAO = new PostingDAO();
        grd_PostingSold.DataSource = saleDAO.GetSaleInfo();
        grd_PostingSold.DataBind();
    }

    public void onCheckedChange(object sender, EventArgs e)
    {
        if (((CheckBox)sender).Checked)
            foreach (GridViewRow row in grd_PostingSold.Rows)
            {
                ((CheckBox)row.FindControl("chk_Item")).Checked = true;
            }
        else
        {
            foreach (GridViewRow row in grd_PostingSold.Rows)
            {
                ((CheckBox)row.FindControl("chk_Item")).Checked = false;
            }
        }
    }
    protected void btn_Delete_Click1(object sender, EventArgs e)
    {
        try
        {
            CompetitionInfo competInfo = new CompetitionInfo();
            CompetitionDAO competDAO = new CompetitionDAO();
            PostingDAO postDAO = new PostingDAO();
            SaleDAO DAOSale = new SaleDAO();
            SaleInfo saleInfo = new SaleInfo();
            bool ischeck = false;

            foreach (GridViewRow row in grd_PostingSold.Rows)
            {
                if (((CheckBox)row.FindControl("chk_Item")).Checked == true)
                {
                    // ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Please choose the exhibition to delete!');", true);
                    ischeck = true;
                }

            }

            if (ischeck == true)
            {
                foreach (GridViewRow row in grd_PostingSold.Rows)
                {
                    int saleID = int.Parse(grd_PostingSold.DataKeys[row.RowIndex].Value.ToString());

                    if (((CheckBox)row.FindControl("chk_Item")).Checked == true)
                    {

                        if (DAOSale.Delete(saleID) > 0)
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete successfull!');", true);
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete Failed!');", true);

                        }
                    }

                }
                LoadData();
            }
            else { ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Please choose the Competition to delete!');", true); }
        }
        catch (Exception ex) { }

    }
}